 ///
 /// @file    figure.cc
 /// @author  lemon(haohb13@gmail.com)
 /// @date    2016-03-15 11:04:12
 ///
 
#include <math.h>
#include <iostream>
using std::cout;
using std::endl;

const float PI = 3.14159;

class Figure
{
public:
	virtual float area()=0;
	virtual void display()=0;
	virtual void func1()=0;
	virtual void func2()=0;
};

class Circle : public Figure
{
public:
	Circle(float radius = 0)
	: _fRadius(radius)
	{
		cout << "Circle(float)" << endl;
	}

	float area()
	{
		return PI * _fRadius * _fRadius;
	}

	void func1()
	{}

	void func2()
	{}

#if 0
	//对于抽象类的派生类，如果有一个纯虚函数没有实现，
	//那么该派生类也会变成一个抽象类
	void display()
	{
		cout << "圆:";
	}
#endif
private:
	float _fRadius;
};


class Rectangle : public Figure
{
public:
	Rectangle(float x, float y)
	: _fx(x)
	, _fy(y)
	{
		cout << "Rectangle(float, float)" << endl;
	}

	float area()
	{
		return _fx * _fy;
	}

	void display()
	{
		cout << "矩形：";
	}
	void func1()
	{}

	void func2()
	{}
private:
	float _fx;
	float _fy;
};


class Triangle : public Figure
{
public:
	Triangle(float x, float y, float z)
	: _fx(x)
	, _fy(y)
	, _fz(z)
	{
		cout << "Triangle(float,float,float)" << endl;
	}

	float area()
	{
		float p = (_fx + _fy + _fz);
		return sqrt(p * (p - _fx) * (p - _fy) * (p - _fz));
	}

	void display()
	{
		cout << "三角形:";
	}
	void func1()
	{}

	void func2()
	{}

private:
	float _fx;
	float _fy;
	float _fz;
};

void test(Figure * pfig)
{
	pfig->display();
	cout << pfig->area() << endl;
}


int main(void)
{
	//Figure f;
	//Figure * pf = new Figure;
	
	Figure * pFig = NULL;

	Circle circle(3);
	Rectangle rectangle(1.2f, 3.6f);
	Triangle triangle(6, 7, 8);

	test(&circle);
	test(&rectangle);
	test(&triangle);

#if 0
	pFig = &circle;
	pFig->display();
	cout << "圆的面积："<< pFig->area() << endl;

	pFig = &rectangle;
	pFig->display();
	cout << "矩形的面积：" << pFig->area() << endl;

	pFig = &triangle;
	pFig->display();
	cout << "三角形的面积:" << pFig->area() << endl;
#endif

	return 0;
}


